package course.web;

import java.security.Principal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import course.domain.User;
import course.repository.UserRepository;
import course.util.*;
import course.repository.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import lombok.extern.slf4j.Slf4j;
import course.domain.Course;
import course.domain.Personal;
import course.domain.CourseAttribute;
import course.domain.CourseAttribute.Type;


@Slf4j
@Controller
@RequestMapping("/selection")
@SessionAttributes(value="personal")
public class SelectionController {
	private final CourseAttributeRepository CourseAttributeRepo;
	private CourseRepository CourseRepo;
	private UserRepository userRepo;
	private PersonalRepository PersonalRepo;
	
	@Autowired
	  public SelectionController(
			  CourseAttributeRepository CourseAttributeRepo, 
	        CourseRepository CourseRepo,
	        PersonalRepository PersonalRepo,
	        UserRepository userRepo) {
	    this.CourseAttributeRepo = CourseAttributeRepo;
	    this.CourseRepo = CourseRepo;
	    this.PersonalRepo=PersonalRepo;
	    this.userRepo=userRepo;
	  }
	

	@ModelAttribute
	public void setCourse(Model model) {
		List<CourseAttribute> CourseAttributes = new ArrayList<>();
		CourseAttributeRepo.findAll().forEach(i -> CourseAttributes.add(i));
		Type[] types = CourseAttribute.Type.values();
		model.addAttribute("types", types);
		for (Type type : types) {
		  model.addAttribute(type.toString(),
		      filterByType(CourseAttributes, type));
		}
		
	}
	
	
	@GetMapping
	public String showselectionForm(Model model,Principal principal) {
		model.addAttribute("list",new Course());
		 String username=principal.getName();
		    User user=userRepo.findByUsername(username);
		    model.addAttribute("user",user);
		return "selection";
	}
	@PostMapping
	public String  processCourse(@Valid @ModelAttribute("list")Course selection,Model model, Errors errors,@SessionAttribute("personal") Personal personal,SessionStatus sessionStatus) {
		if(errors.hasErrors()) {
			return "selection";
		}
		Course saved =CourseRepo.save(selection);
		personal.addselection(saved);
		Personal per= PersonalRepo.save(personal);
		log.info("已选课程: " + selection);
		log.info("个人信息: " + per);
		log.info("课程: " + saved);
		sessionStatus.setComplete();
		return "redirect:/result";
	}
	
	 public List<CourseAttribute> filterByType(
		      List<CourseAttribute> CourseAttributes, Type type) {
		    return CourseAttributes
		              .stream()
		              .filter(x -> x.getType().equals(type))
		              .collect(Collectors.toList());
		  }
	


}